
package com.scalar.chiptrack.adaptor.umc;

import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;
import java.net.URL;
import Acme.HtmlObserver;


public class UMCHtmlObserver implements HtmlObserver {
    
    private String m_downloadURL = null;
    private String m_dynamicWipSummaryDownloadURL = null;
    private String m_dynamicShippingInfoDownloadURL = null;
    private String m_maskInventoryDownloadURL = null;
    private String m_orderSummaryDownloadURL = null;
    private ArrayList m_wipReportUrlList = new ArrayList();
    private ArrayList m_wipReportDateTimeList = new ArrayList();
     
    private ArrayList m_lotBinSummaryUrlList = new ArrayList();
    
   
    public String getDynamicWipSummaryDownloadURL() {
        return m_dynamicWipSummaryDownloadURL;
    }

    public String getDynamicShippingInfoDownloadURL() {
        return m_dynamicShippingInfoDownloadURL;
    }
   
    public String getMaskInventoryDownloadURL() {
        return m_maskInventoryDownloadURL;
    }
    
    public String getOrderSummaryDownloadURL() {
        return m_orderSummaryDownloadURL;
    }
    
    public String getDownloadURL() {
        return m_downloadURL;
    }

    public void setDownloadURL(String m_downloadURL) {
        this.m_downloadURL = m_downloadURL;
    }

    public ArrayList getWipReportUrlList() {
        return m_wipReportUrlList;
    }
    
    public ArrayList getWipReportDateTimeList() {
        return m_wipReportDateTimeList;
    }
    
    //Added by Mallikarjun Ankati Jan 13th 2003
    public ArrayList getLotBinSummaryUrlList(){
	  return m_lotBinSummaryUrlList;
    }
    
    public void gotAHREF (String urlString, URL contextUrl, Object clientData) {

        if (urlString == null) return;

        if ((urlString.indexOf ("open_file.asp") != -1) && (urlString.indexOf("ptype=DOWNLOAD") != -1) && (urlString.indexOf("ppath=WIP/FDYWIP") != -1)) {
            parseWipReportDownloadLink(urlString);
        }
	  	else if((urlString.indexOf ("open_file.asp") != -1) && (urlString.indexOf("ptype=DOWNLOAD") != -1) && (urlString.indexOf("ppath=YIELD/CP/BIN_SUM") != -1)){
			parseLotBinSummaryDownloadLink(urlString);
        }        
        else if (urlString.indexOf ("download.asp") != -1) {
        
            m_downloadURL = urlString.trim();
            if (m_downloadURL.startsWith ("../")) {
                m_downloadURL = "/cisweb" + m_downloadURL.substring(2);
            }
            
            //System.out.println ("URL String = " + urlString);
            //System.out.println ("Download URL String = " + m_downloadURL);
            
            if (urlString.indexOf ("file=WIP_DYNAMIC") != -1) {
                m_dynamicWipSummaryDownloadURL = m_downloadURL;
            }
            else if (urlString.indexOf ("file=SALES_DYNAMIC") != -1) {
                m_dynamicShippingInfoDownloadURL = m_downloadURL;
            }
            else if (urlString.indexOf ("file=MaskInventory_QUERY") != -1) {
                m_maskInventoryDownloadURL = m_downloadURL;
            }
            else if (urlString.indexOf ("file=ORDER_SUMMARY") != -1) {
                m_orderSummaryDownloadURL = m_downloadURL;
            }
        }
    }

    
    public void gotAREAHREF (String urlString, URL contextUrl, Object clientData) {
    }

    public void gotBASEHREF (String urlString, URL contextUrl, Object clientData) {
    }

    public void gotBODYBACKGROUND (String urlString, URL contextUrl, Object clientData) {
    }

    public void gotFRAMESRC (String urlString, URL contextUrl, Object clientData) {
    }

    public void gotIMGSRC (String urlString, URL contextUrl, Object clientData) {
    }

    public void gotLINKHREF (String urlString, URL contextUrl, Object clientData) {
    }

    
    private void parseWipReportDownloadLink (String urlString) {
        StringTokenizer st = new StringTokenizer (urlString, "?&");
        st.nextToken(); // open_file.asp
        String fileName = st.nextToken();   // pfile=UMC00%5FBRC%5FWIP%5F1008%5F2130%2ECSV

        int firstIndex = fileName.indexOf ("%5F");
        if (firstIndex == -1) {
            System.out.println ("ERROR!! ERROR!! ERROR!! \nInvalid WIP Report Download link: " + urlString);
            return;
        }

        int secondIndex = fileName.indexOf ("%5F", firstIndex+1);
        if (secondIndex == -1) {
            System.out.println ("ERROR!! ERROR!! ERROR!! \nInvalid WIP Report Download link: " + urlString);
            return;
        }

        int thirdIndex = fileName.indexOf ("%5F", secondIndex+1);
        if (thirdIndex == -1) {
            System.out.println ("ERROR!! ERROR!! ERROR!! \nInvalid WIP Report Download link: " + urlString);
            return;
        }

        int lastIndex = fileName.indexOf ("%5F", thirdIndex+1);
        if (lastIndex == -1) {
            System.out.println ("ERROR!! ERROR!! ERROR!! \nInvalid WIP Report Download link: " + urlString);
            return;
        }

        String date = fileName.substring (thirdIndex+3, thirdIndex+7);
        String time = fileName.substring (lastIndex+3, lastIndex+7);

        m_wipReportUrlList.add ("/cisweb/logistics/static_rpt/" + urlString);
        m_wipReportDateTimeList.add (date + time);
    }

    //Added by Mallikarjun Ankati Jan 13th 2003
    private void parseLotBinSummaryDownloadLink(String urlString){

	  urlString = urlString.replaceAll("%5F", "_");
	  urlString = urlString.replaceAll("%2D", "-");
	  urlString = urlString.replaceAll("%2E", ".");

	  m_lotBinSummaryUrlList.add("/cisweb/quality/static_rpt/"+urlString);
    }
}